<template>
  <!-- 组织管理/添加人员 -->
  <div class="sub-menu">
    <el-dialog
      title="添加人员"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <div>
        <el-form :inline="true" :model="formData">
          <el-form-item label="公司">
            <el-select v-model="formData.id" clearable placeholder="请选择">
              <el-option
                v-for="item in conpanyArr"
                :key="item.companyId"
                :label="item.orgName"
                :value="item.companyId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分配状态">
            <el-select
              v-model="formData.relationStatus"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in relationStatusArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="检索">
            <el-input
              v-model="formData.keyword"
              clearable
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="formLoading"
              type="primary"
              @click.stop="searchForm()"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <el-editable
          v-loading="addUserLoading"
          :data="menuList"
          height="200"
          :columns="addUserListColumns"
          @selection-change="handleSelectionChange"
        >
          <template slot="default-change" slot-scope="scope">
            <div>
              <el-radio v-model="tableRadio" :label="scope.$index"
                ><i></i
              ></el-radio>
            </div>
          </template>
          <template slot="default-appLogo" slot-scope="scope">
            <div class="logo-box">
              <icon-font
                class="icon-item"
                :icon="scope.row.appLogo || 'el-icon-setting'"
              />
            </div>
          </template>
        </el-editable>
        <el-pagination
          align="right"
          style="margin-top: 10px;"
          @size-change="sizeChange"
          @current-change="pageChange"
          :page-sizes="PAGE_NUMBER"
          :page-size="page.size"
          :current-page="page.page"
          :layout="PAGINATION_LAYOUT"
          :total="page.total"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import ElEditable from '@components/el-editable/door/index.vue'
import {PAGINATION_LAYOUT, PAGE_NUMBER} from '@const/common.js'
import {hasIn, cloneDeep} from 'lodash'
import {
  UcOrganizationUserList,
  resourceUpdateStatusById,
  UcOrganizationCompanyList, // 查询当前公司关联的集团下的公司列表
  appResourceAdd,
  UcOrganizationAddUserList, // 批量增高岗位员工
  menuDeleteById
} from '@const/api_staffCenter.js'
import {addUserListColumns} from './const.js'
export default {
  components: {ElEditable},
  data() {
    return {
      dialogVisible: false,
      radio: '1',
      tableRadio: '',
      formData: {
        id: '',
        keyword: '',
        relationStatus: ''
      },
      formDataNav: {
        keyword: null
      },
      formLoading: false,
      addUserLoading: false,
      menuList: [],
      page: {
        size: 20,
        page: 1,
        total: 0
      },
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      addUserListColumns,
      curSelectNode: {},
      multipleSelection: [],
      relationStatusArr: [
        {
          value: '0',
          label: '未关联'
        },
        {
          value: '1',
          label: '已关联'
        }
      ],
      conpanyArr: []
    }
  },
  mounted() {},
  methods: {
    showDialog(curSelectNode) {
      this.curSelectNode = curSelectNode
      this.dialogVisible = true
      this.search()
      this.getCompanyList()
    },
    handleClose(done) {
      done()
    },
    pageChange(val) {
      this.page.page = val
      this.search()
    },
    sizeChange(val) {
      this.page.size = val
      this.search()
    },
    handleSelectionChange(data) {
      this.multipleSelection = data
    },
    submitForm() {
      let userList = []
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择人员')
        return false
      }
      this.multipleSelection.forEach(item => {
        userList.push(item.userId)
      })
      let params = {
        userIdList: userList,
        orgId: this.curSelectNode.id,
        companyId: this.curSelectNode.companyId
      }
      this.$axios({
        url: UcOrganizationAddUserList,
        method: 'post',
        data: params
      })
        .then(res => {
          if (res.data.msg === 'ok') {
            this.$message.success('新增成功')
            this.$emit('getUserList', this.curSelectNode.id)
          }
        })
        .finally(() => {
          this.dialogVisible = false
        })
    },
    searchForm() {
      this.page.page = 1
      this.search()
    },
    search() {
      this.addUserLoading = true
      this.$axios({
        url: `${UcOrganizationUserList}?page=${this.page.page}&pageSize=${
          this.page.size
        }&relationStatus=${this.formData.relationStatus}&keyword=${
          this.formData.keyword
        }&companyId=${this.formData.id || this.curSelectNode.companyId}`,
        method: 'get'
      })
        .then(res => {
          let data = hasIn(res, ['data', 'payload']) ? res.data.payload : []
          this.menuList = data.content
          this.page.total = data.totalElements
        })
        .finally(() => {
          this.addUserLoading = false
        })
    },
    getCompanyList() {
      this.$axios({
        url: `${UcOrganizationCompanyList}?id=${this.curSelectNode.id}&orgName=${this.curSelectNode.orgName}&orgType=${this.curSelectNode.orgType}`,
        method: 'GET'
      })
        .then(res => {
          if (res.data.code === '0') {
            this.conpanyArr = res.data.payload
          }
        })
        .finally(() => {})
    }
  }
}
</script>
<style lang="less"></style>
